import { purtcPurchaseDetail, report } from '@/api/url/salesOrderManagement';
import router from '@/router';
import { EFTimeFormatConversion } from '@/utils/baseMethods';
import { nextTick, onMounted, onUnmounted, reactive } from 'vue';
import notification from '@arco-design/web-vue/es/notification';
import { completeTask } from '@api/url/processManagement';

export default function importConfig() {
  const base = reactive<any>({
    tabelList: null,
    topTabel: null,
    bottomTabel: null,
    sendWorkTabel: null,
    descriptionsRef: null,
    tableHeight: '',
    from: 'purchase',
    taskID: null,
  });

  const init = () => {
    getTable();
  };
  /**
   * @exports viewDetails 查看采购单详情
   */
  const getTable = () => {
    const param = router.currentRoute.value.query;
    if (param.type) {
      //从采购订单页面进入当前页
      base.from = 'purchase';
      const steam = {
        TC001: param.type,
        TC002: param.id,
      };
      purtcPurchaseDetail(steam).then((res: any) => {
        if (res.code == 200) {
          base.topTabel = res.data;
          base.bottomTabel = res.data.purtdList;
          sendWorkOption();
        }
      });
    } else {
      //从用户任务页面进入当前页
      base.from = 'task';
      const res = JSON.parse(localStorage.getItem('currentUserTaskData'));
      base.taskID = res.taskID;
      base.topTabel = res.data;
      base.bottomTabel = res.data.purtdList;
      sendWorkOption();
    }
  };

  /**
   * @exports sendWorkOption 采购单信息嫁接
   */
  const sendWorkOption = () => {
    base.sendWorkTabel = [];
    base.sendWorkTabel = [
      {
        label: '请购部门',
        value: base.topTabel.purchaseDate,
      },
      {
        label: '部门名称',
        value: base.topTabel.purchaseDepartmentName,
      },
      {
        label: '采购单号',
        value: base.topTabel.type + '-' + base.topTabel.id,
      },
      {
        label: '采购人员',
        value: base.topTabel.ProcurementStaffname,
      },
      {
        label: '采购日期',
        value: EFTimeFormatConversion(base.topTabel.purchaseDate),
      },
      {
        label: '供应商编号',
        value: base.topTabel.supplier,
      },
      {
        label: '供应商名称',
        value: base.topTabel.supplierName,
      },
      {
        label: '单据日期',
        value: EFTimeFormatConversion(base.topTabel.documentDate),
      },
      {
        label: '交易币种',
        value: base.topTabel.coinType,
      },
      {
        label: '汇率',
        value: base.topTabel.exchangeRate,
      },
      {
        label: '税额',
        value: base.topTabel.totalTax,
      },
      {
        label: '税前总金额',
        value: base.topTabel.purchaseAmount,
      },
      {
        label: '总金额',
        value: base.topTabel.totalAmount,
      },
      {
        label: '总数量',
        value: base.topTabel.totalNumber,
      },
      {
        label: '审核者',
        value: base.topTabel.userName,
      },
      {
        label: '创建者',
        value: base.topTabel.creator,
      },
      {
        label: '付款条件',
        value: base.topTabel.paymentName,
      },
      {
        label: '收货地址',
        value: base.topTabel.shippingAddressOne,
      },
      {
        label: '备注',
        value: base.topTabel.remark,
      },
    ];
  };

  //上报
  const escalation = () => {
    const userName = localStorage.getItem('userName');
    report(base.topTabel, userName).then((res) => {
      notification.info({
        title: '',
        content: res.data,
      });
      if (res.code == 200) {
        router.go(-1);
      }
    });
  };
  //监听高度变化
  const handleResize = () => {
    const height1 = base.descriptionsRef.clientHeight;
    const height = height1 + 200;
    base.tableHeight = `calc(100vh - ${height}px)`;
  };

  //是否同意完成
  const complete = (remark) => {
    const obj = {
      remark: remark,
      taskID: base.taskID,
    };
    completeTask(obj).then((res) => {
      if (res.code == 200) {
        notification.info('操作成功');
        router.go(-1);
      } else {
        notification.info('失败');
      }
    });
  };

  //当前用户任务同意
  const agree = () => {
    complete('同意');
  };
  //当前用户任务不同意
  const disagree = () => {
    complete('不同意');
  };

  onMounted(() => {
    setTimeout(() => {
      handleResize();
    }, 1000);
    window.addEventListener('resize', handleResize);
  });

  onUnmounted(() => {
    window.removeEventListener('resize', handleResize);
  });

  return {
    base,
    init,
    escalation,
    agree,
    disagree,
  };
}
